package com.piece.core.oauth.config;

import com.piece.core.oauth.properties.JustAuthProperties;
import com.piece.core.oauth.social.AuthRequestFactory;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 鉴权、社交自动配置
 */
public class AuthAutoConfig {

    @Bean("passwordEncoder")
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
    public AuthRequestFactory authRequestFactory(JustAuthProperties properties) {
        return new AuthRequestFactory(properties, AuthDefaultStateCache.INSTANCE);
    }
}